import store from '@/store'
import { pad } from 'crypto-js'
export default function ContextPadProvider(contextPad, config, injector, translate, bpmnFactory, elementFactory, create, modeling, connect) {
    this.create = create
    this.elementFactory = elementFactory
    this.translate = translate
    this.bpmnFactory = bpmnFactory
    this.modeling = modeling
    this.connect = connect
    config = config || {}
    if (config.autoPlace !== false) {
        this.autoPlace = injector.get('autoPlace', false);
    }
    contextPad.registerProvider(this)
}

ContextPadProvider.$inject = [
    'contextPad',
    'config',
    'injector',
    'translate',
    'bpmnFactory',
    'elementFactory',
    'create',
    'modeling',
    'connect'
]

ContextPadProvider.prototype.getContextPadEntries = function (element) {
    const {
        autoPlace,
        create,
        elementFactory,
        translate,
        modeling,
        bpmnFactory,
        connect
    } = this;
    var actions = {}
    var businessObject = element.businessObject
    // 删除功能
    function removeElement(e) {
        modeling.removeElements([element])
    }
    function startConnect(event, element) {
        connect.start(event, element)
    }
    function clickElement(e) {
        console.log(element)
        // window.localStorage.setItem('nodeInfo', JSON.stringify(element))
        // window.localStorage.setItem('nodeVisible', 'true')
        store.commit('SETNODEINFO', element)
        store.commit('TOGGLENODEVISIBLE', true)
    }

    function appendTask1(event, element) {
        // console.log(autoPlace)
        if (autoPlace) {
            const businessObject = bpmnFactory.create('bpmn:Task', { custom: 1, text: '测试用', title: '测试标题', unit: '责任单位' });
            const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });
            autoPlace.append(element, shape);
        } else {
            appendTaskStart1(event, element);
        }
    }

    function appendTaskStart1(event, element) {
        // console.log(event)
        const businessObject = bpmnFactory.create('bpmn:Task', { custom: 1, text: '测试用', title: '测试标题', unit: '责任单位' });
        const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });

        create.start(event, shape, element);
    }

    function appendTask2(event, element) {
        // console.log(autoPlace)
        if (autoPlace) {
            const businessObject = bpmnFactory.create('bpmn:Task', { custom: 2, text: '测试用', title: '测试标题', unit: '责任单位' });
            const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });
            autoPlace.append(element, shape);
        } else {
            appendTaskStart2(event, element);
        }
    }

    function appendTaskStart2(event, element) {
        // console.log(event)
        const businessObject = bpmnFactory.create('bpmn:Task', { custom: 2, text: '测试用', title: '测试标题', unit: '责任单位' });
        const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });

        create.start(event, shape, element);
    }

    function appendTask3(event, element) {
        // console.log(autoPlace)
        if (autoPlace) {
            const businessObject = bpmnFactory.create('bpmn:Task', { custom: 3, text: '测试用', title: '测试标题', unit: '责任单位' });
            const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });
            autoPlace.append(element, shape);
        } else {
            appendTaskStart3(event, element);
        }
    }

    function appendTaskStart3(event, element) {
        // console.log(event)
        const businessObject = bpmnFactory.create('bpmn:Task', { custom: 3, text: '测试用', title: '测试标题', unit: '责任单位' });
        const shape = elementFactory.createShape({ type: 'bpmn:Task', businessObject });

        create.start(event, shape, element);
    }

    function deleteElement() {
        return {
            group: 'tools',
            className: 'icon-custom icon-custom-delete',
            title: translate('删除'),
            action: {
                click: removeElement
            }
        }
    }

    // 不同类型的元素context pad不同，连线只能删除
    if (businessObject.$type == 'bpmn:Task') {
        Object.assign(actions, {
            // 'append.lindaidai-task': {
            //     group: 'model',
            //     className: 'icon-custom lindaidai-task',
            //     title: translate('创建一个类型为circle-task的任务节点'),
            //     action: {
            //         click: appendTask1,
            //         dragstart: appendTaskStart1
            //     }
            // },
            // 'append.rect-task': {
            //     group: 'model',
            //     className: 'icon-custom rect-task',
            //     title: translate('创建一个类型为rect-task的任务节点'),
            //     action: {
            //         click: appendTask2,
            //         dragstart: appendTaskStart2
            //     }
            // },
            // 'append.polygon-task': {
            //     group: 'model',
            //     className: 'icon-custom polygon-task',
            //     title: translate('创建一个类型为polygon-task的任务节点'),
            //     action: {
            //         click: appendTask3,
            //         dragstart: appendTaskStart3
            //     }
            // },
            'append.flow': {
                group: 'tools',
                className: 'icon-custom icon-custom-flow',
                title: translate('链接'),
                action: {
                    click: startConnect,
                    dragstart: startConnect
                }
            },
            'delete': deleteElement(),
        })
    }
    if ((businessObject.$type == 'bpmn:SequenceFlow')) {
        Object.assign(actions, {
            'delete': deleteElement()
        })
    }

    return actions
}